<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :showInput="true"
          :show="false"
          @search="handleSearch"
        />

        <el-select
          style="margin-left: 10px; width: 120px;"
          v-model="query.entranceStatus"
          placeholder="状态选择"
          class="handle-select mr10"
          @change="checkStatus"
          clearable
        >
          <el-option
            v-for="(item, index) in entranceStatusArr"
            :key="index"
            :label="item"
            :value="index"
          ></el-option>
        </el-select>
        <el-button type="default" class="handle-del" @click="handleSearch()"
          >查询</el-button
        >

        <el-button type="default" @click="handleIncrease">新增</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="序号"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="subDistrict"
          align="center"
          label="街道"
        ></el-table-column>

        <el-table-column
          prop="communityName"
          align="center"
          label="社区"
        ></el-table-column>

        <el-table-column
          prop="name"
          align="center"
          label="小区"
        ></el-table-column>

        <el-table-column
          prop="totalQuantity"
          align="center"
          label="总户数"
        ></el-table-column>

        <el-table-column
          prop="actualQuantity"
          align="center"
          label="实际入住户数"
        ></el-table-column>

        <el-table-column
          prop="provideQuantity"
          align="center"
          label="发桶数"
        ></el-table-column>

        <el-table-column
          prop="statusName"
          align="center"
          label="入场状态"
        ></el-table-column>

        <el-table-column
          prop="entranceDate"
          align="center"
          label="入场时间"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="handleApproach(scope.$index, scope.row, 1)"
              >进场</el-button
            >
            <el-button
              type="text"
              @click="handleExit(scope.$index, scope.row, 2)"
              >退场</el-button
            >
            <el-button
              type="text"
              @click="handleLivingRoom(scope.$index, scope.row)"
              >户室管理</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
      <el-form
        ref="editForm"
        :model="editForm"
        label-width="auto"
        :rules="rules"
      >
        <el-form-item label="请选择街道" prop="value">
          <template>
            <el-select
              v-model="value"
              placeholder="请选择"
              @change="boundChange"
            >
              <el-option
                v-for="item in streetList1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="请选择社区" prop="value1">
          <template>
            <el-select
              v-model="value1"
              placeholder="请选择"
              @change="boundChange1"
              @visible-change="visibleChangeCommunity1"
            >
              <el-option
                v-for="item in communityList1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="请输入小区" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item label="发桶数">
          <el-input v-model="editForm.provideQuantity"></el-input>
        </el-form-item>

        <el-form-item label="入场状态" prop="entranceStatus">
          <template>
            <el-radio-group v-model="editForm.entranceStatus">
              <el-radio :label="0">未进场</el-radio>
              <el-radio :label="1">已进场</el-radio>
              <el-radio :label="2">已退场</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>

        <el-form-item label="入场时间">
          <div class="block">
            <el-date-picker
              v-model="editForm.entranceDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="addForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="请选择街道" prop="value">
          <template>
            <el-select
              v-model="value"
              placeholder="请选择"
              @change="boundChange"
            >
              <el-option
                v-for="item in streetList1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="请选择社区" prop="value1">
          <template>
            <el-select
              v-model="value1"
              placeholder="请选择"
              @change="boundChange1"
              @visible-change="visibleChangeCommunity1"
            >
              <el-option
                v-for="item in communityList1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item label="请填写小区" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>

        <el-form-item label="发桶数">
          <el-input v-model="addForm.provideQuantity"></el-input>
        </el-form-item>

        <el-form-item label="入场状态" prop="entranceStatus">
          <template>
            <el-radio-group v-model="addForm.entranceStatus">
              <el-radio :label="0">未进场</el-radio>
              <el-radio :label="1">已进场</el-radio>
              <el-radio :label="2">已退场</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item label="入场时间">
          <div class="block">
            <el-date-picker
              v-model="addForm.entranceDate"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelIncrease">取 消</el-button>
        <el-button type="primary" @click="saveIncrease">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from "utils/util";
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
export default {
  name: "communitymanage",
  data() {
    return {
      streetList1: [],
      streetList: [],
      subDistrictId: "",
      communityList1: [],
      communityList: [],

      villageList: [],

      value: "",
      value1: "",
      ids: [],
      entranceStatusArr: ["未进场", "已进场", "已退场"],
      query: {
        pageIndex: 1,
        pageSize: 20,
        entranceStatus: "",

        streetId: "",
        communityId: "",
        villageId: "",

        streetName: "",
        communityName: "",
        villageName: "",
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      form: {},
      addForm: {
        parentId: "",
        name: "",
        provideQuantity: "",
        entranceStatus: "",
        entranceDate: "",
      },
      editForm: {
        id: "",
        name: "",
        provideQuantity: "",
        entranceStatus: "",
        entranceDate: "",
        parentId: "",
      },
      rules: {
        value: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        value1: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        name: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        entranceStatus: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        // required: [{ required: true, message: "必填不能为空", trigger: "blur" }]
      },
      idx: -1,
    };
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  created() {
    this.getData();
  },
  methods: {
    ...commonItem1,
    getData() {
      this.$axios
        .get("/village/village/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            entranceStatus: this.query.entranceStatus,
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            villageName: this.query.villageName,
          },
        })
        .then(({ data: res }) => {
          console.log("小区管理", res);
          if (res.code != 0) {
            return;
          }
          if (res.data) {
            res.data.list.map((item, index) => {
              item.entranceDate = this.dateFormat(item.entranceDate);
              var pageIndex = this.query.pageIndex;
              item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
              if (item.entranceStatus == 0) {
                item.statusName = "未进场";
              } else if (item.entranceStatus == 1) {
                item.statusName = "已进场";
              } else if (item.entranceStatus == 2) {
                item.statusName = "已退场";
              }
            });

            this.tableData = res.data.list;
            this.pageTotal = res.data.total || 0;
          } else {
            this.tableData = [];
            this.pageTotal = 0;
          }
        })
        .catch(() => {});
    },
    // 新增操作
    handleIncrease() {
      this.addVisible = true;
      // 获取街道
      this.getStreetList1();
    },
    visibleChangeCommunity1(bool) {
      if (bool) {
        this.getCommunityList1();
      }
      console.log(bool);
    },
    getCommunityList1() {
      if (!this.subDistrictId) {
        return this.$message.warning({
          message: "请先选择街道",
          duration: 1500,
        });
      }

      return this.$axios
        .get("/village/subdistrict/page", {
          params: {
            parentId: this.subDistrictId,
          },
        })
        .then(({ data: res }) => {
          console.log("社区数据", res);
          if (res.code != 0) {
            return;
          }
          this.communityList1 = res.data.list;
        })
        .catch(() => {});
    },
    getStreetList1() {
      return this.$axios
        .get("/village/subdistrict/page", {
          params: {
            parentId: "0",
          },
        })
        .then(({ data: res }) => {
          console.log("街道数据", res);
          if (res.code != 0) {
            return;
          }
          this.streetList1 = res.data.list;
        })
        .catch(() => {});
    },
    boundChange(subDistrictId) {
      console.log("街道ID", subDistrictId);
      this.subDistrictId = subDistrictId;
      this.value1 = "";
      // 获取社区
      this.getCommunityList1();
    },
    boundChange1(communityId) {
      console.log("社区ID", communityId);
      if (this.addVisible) {
        this.addForm.parentId = communityId;
      } else if (this.editVisible) {
        this.value1 = this.editForm.parentId = communityId;
      }
    },

    cancelIncrease() {
      this.addVisible = false;
      this.addForm = {
        parentId: "",
        name: "",
        provideQuantity: "",
        entranceStatus: "",
        entranceDate: "",
      };

      this.subDistrictId = "";

      this.value = "";
      this.value1 = "";
    },
    // 保存新增
    saveIncrease() {
      console.log(this.addForm);
      this.addForm.entranceDate =
        typeof this.addForm.entranceDate == "object"
          ? this.addForm.entranceDate.getTime()
          : "";
      var bool = util.isEmpty({
        parentId: this.addForm.parentId,
        name: this.addForm.name,
        entranceStatus: this.addForm.entranceStatus,
      });
      console.log(bool);
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }

      this.$axios
        .post("/village/village", {
          ...this.addForm,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error(res.msg);
          }
          this.addVisible = false;
          this.$message.success("操作成功");
          this.addForm = {
            parentId: "",
            name: "",
            provideQuantity: "",
            entranceStatus: "",
            entranceDate: "",
          };

          this.subDistrictId = "";
          this.value = "";
          this.value1 = "";

          this.getData();
        });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      console.log(ids);
      this.$axios
        .delete("/village/village", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getData();
        })
        .catch(() => {});
    },

    checkStatus(status) {
      // console.log(status);
      // this.query.streetId = "";
      // this.query.communityId = "";
      // this.query.villageId = "";
      // this.query.villageName = "";

      // this.$set(this.query, "entranceStatus", status);
      // this.$set(this.query, "pageIndex", 1);
      // this.getData();
    },

    getEditInfo(id) {
      this.$axios
        .get(`/village/village/${id}`)
        .then(({ data: res }) => {
          console.log("编辑数据", res);
          if (res.code != 0) {
            return;
          }

          this.subDistrictId = this.value = res.data.subDistrictId;
          this.value1 = res.data.communityId;

          if (res.data.subDistrictId) {
            // 获取社区数据
            this.getCommunityList1(res.data.subDistrictId).then(() => {
              this.editForm = {
                id: res.data.id,
                name: res.data.name,
                provideQuantity: res.data.provideQuantity,
                entranceStatus: res.data.entranceStatus,
                entranceDate: res.data.entranceDate
                  ? new Date(parseInt(res.data.entranceDate))
                  : "",
                parentId: res.data.parentId,
              };
              console.log(this.editForm);
            });
          }
        })
        .catch(() => {});
    },
    // 进场
    handleApproach(index, row, entranceStatus) {
      console.log(index, row, entranceStatus);
      this.editForm = {
        id: "",
        entranceStatus: "",
      };
      this.$set(this.editForm, "entranceStatus", entranceStatus);
      this.$set(this.editForm, "id", row.id);
      // 二次确认删除
      this.$confirm("确定要改为进场吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.saveEditCopy();
        })
        .catch(() => {});
    },
    // 退场
    handleExit(index, row, entranceStatus) {
      this.editForm = {
        id: "",
        entranceStatus: "",
      };
      this.$set(this.editForm, "entranceStatus", entranceStatus);
      this.$set(this.editForm, "id", row.id);
      // 二次确认删除
      this.$confirm("确定要退场吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.saveEditCopy();
        })
        .catch(() => {});
    },
    saveEditCopy() {
      this.$axios
        .put("/village/village", {
          ...this.editForm,
        })
        .then(({ data: res }) => {
          if (res.code != 0) {
            return this.$message.error({
              duration: 1500,
              message: res.msg,
            });
          }
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          this.editVisible = false;
          this.editForm = {
            id: "",
            name: "",
            provideQuantity: "",
            entranceStatus: "",
            entranceDate: "",
            parentId: "",
          };

          this.getData();
        })
        .catch(() => {});
    },
    // 住室管理
    handleLivingRoom(index, row) {
      console.log(index, row);
      this.$router.push({
        path: "/house-manage-copy",
        query: {
          villageId: row.id,
        },
      });
    },
    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row);
      this.id = row.id;
      this.idx = index;
      this.editVisible = true;
      // 获取街道数据
      this.getStreetList1().then(() => {
        // 获取编辑数据
        this.getEditInfo(row.id);
      });
    },
    cancelEdit() {
      this.editVisible = false;
      this.editForm = {
        id: "",
        name: "",
        provideQuantity: "",
        entranceStatus: "",
        entranceDate: "",
        parentId: "",
      };

      this.subDistrictId = "";

      this.value = "";
      this.value1 = "";
    },
    // 保存编辑
    saveEdit() {
      this.editForm.entranceDate =
        typeof this.editForm.entranceDate == "object"
          ? this.editForm.entranceDate.getTime()
          : "";
      var bool = util.isEmpty({
        parentId: this.editForm.parentId,
        name: this.editForm.name,
        entranceStatus: this.editForm.entranceStatus,
      });
      console.log(this.editForm);
      if (bool) {
        return this.$message.warning("必填项不能为空");
      }

      this.$axios
        .put("/village/village", {
          ...this.editForm,
        })
        .then(({ data: res }) => {
          if (res.code != 0) {
            return;
          }
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
          this.editVisible = false;
          this.editForm = {
            id: "",
            name: "",
            provideQuantity: "",
            entranceStatus: "",
            entranceDate: "",
            parentId: "",
          };

          this.getData();
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
